<?php
namespace app\index\controller;

use app\index\model\ContractCaiModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\LabourGongModel;
use app\index\model\LabourXiangListModel;
use app\index\model\LabourXiangModel;
use app\index\model\LabourYongListModel;
use app\index\model\LabourYongModel;
use app\index\relevan\EnginRelevan;
use app\index\service\CertService;
use app\index\service\EnginService;
use app\index\service\FlowService;
use app\index\service\LabourService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Labour extends Common
{

    public function list_labour_gong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['ling.deleted_user','=', 0];
        $where[] = ['ling.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['ling.name|ling.idcard|ling.tel|ling.areas|ling.bankname|ling.banknum','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['ling.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['idcard']) && !empty($param['idcard'])){
            $where[] = ['ling.idcard','like', '%'.$param['idcard'].'%'];
        }
        if(isset($param['tel']) && !empty($param['tel'])){
            $where[] = ['ling.tel','like', '%'.$param['tel'].'%'];
        }
        if(isset($param['yongxing']) && !empty($param['yongxing'])){
            $where[] = ['ling.yongxing','=', $param['yongxing']];
        }
        if(isset($param['gongtype']) && !empty($param['gongtype'])){
            $where[] = ['ling.gongtype','=', $param['gongtype']];
        }
        if(isset($param['areas']) && !empty($param['areas'])){
            $where[] = ['ling.areas','like', '%'.$param['areas'].'%'];
        }
        if(isset($param['bankname']) && !empty($param['bankname'])){
            $where[] = ['ling.bankname','like', '%'.$param['bankname'].'%'];
        }
        if(isset($param['banknum']) && !empty($param['banknum'])){
            $where[] = ['ling.banknum','like', '%'.$param['banknum'].'%'];
        }

        $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
        $data['is_yongxing'] = isset($param['yongxing']) ? $param['yongxing'] : '';

        $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
        $data['is_gongtype'] = isset($param['gongtype']) ? $param['gongtype'] : '';



        $data['data'] = LabourGongModel::alias('ling')
            ->field('ling.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('labour_gong', 'ling'))
            ->order('ling.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){

                $item['gongtype'] = (new OptService())->get_base_showname('engin_labour_gongtype', $item['gongtype']);

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('labour_gong');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_labour_gong(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['name'])){
                return outputJson(4000, '请输入姓名');
            }
            if(empty($param['idcard'])){
                return outputJson(4000, '请输入身份证号');
            }
            if(!(new CertService())->check_person_idcard_mate($param['idcard'])){
                return outputJson(4000, '身份证号错误');
            }
            if((new LabourService())->check_labour_gong_name($param['name'], $param['idcard'])){
                return outputJson(4000, '人员已存在');
            }

            if(empty($param['tel'])){
                return outputJson(4000, '请输入联系电话');
            }
            if(empty($param['yongxing'])){
                return outputJson(4000, '请选择用工性质');
            }



            $param['sex'] = check_sex_by_idcard($param['idcard']);

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];
            $param['chanid'] = (new UserService())->get_user_chanid();

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'labour_gong') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);


            Db::startTrans();
            try {
                $id = LabourGongModel::insertGetId($param);

                (new OptService())->relation_file($files,'labour_gong',$id);
                (new EnginRelevan())->vate_engin_file('labour_gong', $id);

                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'labour_gong', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'labour_gong', $id, '工人花名册');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['areas'] = (new OptService())->get_city_cader();
            $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
            $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
            $data['files'] = (new OptService())->get_file('labour_gong');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_labour_gong(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['name'])){
                return outputJson(4000, '请输入姓名');
            }
            if(empty($param['idcard'])){
                return outputJson(4000, '请输入身份证号');
            }
            if(!(new CertService())->check_person_idcard_mate($param['idcard'])){
                return outputJson(4000, '身份证号错误');
            }
            if((new LabourService())->check_labour_gong_name($param['name'], $param['idcard'], $param['id'])){
                return outputJson(4000, '人员已存在');
            }
            if(empty($param['tel'])){
                return outputJson(4000, '请输入联系电话');
            }
            if(empty($param['yongxing'])){
                return outputJson(4000, '请选择用工性质');
            }


            $param['sex'] = check_sex_by_idcard($param['idcard']);

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'labour_gong') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);


            Db::startTrans();
            try {
                LabourGongModel::update($param);

                (new OptService())->relation_file($files,'labour_gong',$param['id']);
                (new EnginRelevan())->vate_engin_file('labour_gong', $param['id']);


                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'labour_gong', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'labour_gong', $param['id'], '工人花名册');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = LabourGongModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }


            $data['areas'] = (new OptService())->get_city_cader();
            $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
            $data['gongtype'] = (new OptService())->get_base_info('engin_labour_gongtype');
            $data['files'] = (new OptService())->get_file('labour_gong', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_labour_gong(Request $request){
        $param = $request->post();
        $data = LabourGongModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            LabourGongModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new EnginRelevan())->vate_engin_file('labour_gong', $data['id']);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'labour_gong', $data['id'], '工人花名册');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_labour_gong(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = LabourGongModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['data']['gongtype'] = (new OptService())->get_base_showname('engin_labour_gongtype', $data['data']['gongtype']);
        $data['files'] = (new OptService())->get_file('labour_gong', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_labour_xiang(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['xiang.deleted_user','=', 0];
        $where[] = ['xiang.comid','=',$this->userinfo['comid']];
        $where[] = ['xiang.isturn','=',1];
        $where[] = ['xiang.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['xiang.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['xiang.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['xiang.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['xiang.status','=', $param['status']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = LabourXiangModel::alias('xiang')
            ->field('xiang.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=xiang.enginid')
            ->leftJoin('flow_bill bill','bill.id=xiang.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('labour_xiang', 'xiang'))
            ->order('xiang.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('labour_xiang');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_labour_xiang(Request $request){
        $param = $request->post();
        $data = LabourXiangModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            LabourXiangModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'labour_xiang', $data['id'], '项目花名册');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_labour_xiang(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = LabourXiangModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);

        $list = LabourXiangListModel::where(['xiangid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['gongtype_name'] = (new OptService())->get_base_showname('engin_labour_gongtype', $item['gongtype']);
            $item['gonglei_name'] = (new OptService())->get_base_showname('engin_labour_gonglei', $item['gonglei']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('labour_xiang', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }


    public function list_labour_yong(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['yong.deleted_user','=', 0];
        $where[] = ['yong.comid','=',$this->userinfo['comid']];
        $where[] = ['yong.isturn','=',1];
        $where[] = ['yong.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['e.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['yong.enginid','=', $param['enginid']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['enginnumber']) && !empty($param['enginnumber'])){
            $where[] = ['e.number','like', '%'.$param['enginnumber'].'%'];
        }

        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['yong.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['title']) && !empty($param['title'])){
            $where[] = ['yong.title','like', '%'.$param['title'].'%'];
        }
        if(isset($param['yongxing']) && !empty($param['yongxing'])){
            $where[] = ['yong.yongxing','=', $param['yongxing']];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['yong.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['yong.baner','like', '%'.$param['baner'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }
        if(isset($param['business']) && !empty($param['business'])){
            $where[] = ['e.business','=', $param['business']];
        }

        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['yong.status','=', $param['status']];
        }


        $data['yongxing'] = (new LabourService())->get_labour_gong_yongxing();
        $data['is_yongxing'] = isset($param['yongxing']) ? $param['yongxing'] : '';

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['data'] = LabourYongModel::alias('yong')
            ->field('yong.*,e.name as enginname,bill.sericnum,bill.modename')
            ->join('engin e','e.id=yong.enginid')
            ->leftJoin('flow_bill bill','bill.id=yong.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('labour_yong', 'yong'))
            ->order('yong.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['caiid_name'] = '';
                if($item['yongxing'] == '劳务分包'){
                    $item['caiid_name'] = ContractCaiModel::where(['id'=>$item['caiid']])->value('name');
                }
                $item['yongnums'] = LabourYongListModel::where(['yongid'=>$item['id']])->count();
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('labour_yong');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_labour_yong(Request $request){
        $param = $request->post();
        $data = LabourYongModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            LabourYongModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'labour_yong', $data['id'], '用工单');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_labour_yong(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = LabourYongModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['engininfo'] = (new EnginService())->get_engin_showinfo($data['data']['enginid']);
        $data['caiidinfo'] = ContractCaiModel::get_info_by_pk($data['data']['caiid']);



        $list = LabourYongListModel::where(['yongid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['gongtype_name'] = (new OptService())->get_base_showname('engin_labour_gongtype', $item['gongtype']);
            $item['gonglei_name'] = (new OptService())->get_base_showname('engin_labour_gonglei', $item['gonglei']);
        }
        $data['list'] = $list;

        $data['files'] = (new OptService())->get_file('labour_yong', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }




}
